package emulator.hardware.io.peripherals.iec;

import java.util.Observable;

public class DeviceInfo extends Observable {
	final static public int DISK = 0;
	final static public int PRINTER = 1;
	final static private String[] device_types = { "disk", "printer" };   
	private boolean is_enabled = false;
	private int address = 0;
	private String type = "";
	private String file = "";
	
	public DeviceInfo(int address, String type, String file)
	{
		this.address = address;
		this.type = type;
		this.file = file;
		this.is_enabled = true;
	}
	
	public DeviceInfo(int address, int type, String file) {
		this.address = address;
		this.type = device_types[type];
		this.file = file;
		this.is_enabled = true;
	}

	public static String[] getDeviceTypes()
	{
		return device_types;
	}
	
	public void setEnabled(boolean is_enabled) {
		this.is_enabled = is_enabled;
		setChanged();
		notifyObservers();
	}
	
	public boolean isEnabled() {
		return is_enabled;
	}
	
	public void setAddress(int address) {
		this.address = address;
		setChanged();
		notifyObservers();
	}
	
	public int getAddress() {
		return address;
	}
	
	public void setType(String type) {
		this.type = type;
		setChanged();
		notifyObservers();
	}
	
	public String getType() {
		return type;
	}
	
	public void setFile(String file) {
		this.file = file;
		setChanged();
		notifyObservers();
	}
	
	public String getFile() {
		return file;
	}

	public static DeviceInfo fromConfig(String device_config) {
		String[] parts = device_config.split(",");
		if (parts.length >= 4)
		{
			int address = Integer.parseInt(parts[0]);
			DeviceInfo info = new DeviceInfo(address, parts[1], parts[2]);
			info.is_enabled = parts[3].equalsIgnoreCase("active");
			return info;
		}
		return null;
	}
	
	public String toConfig()
	{
		return address+","+type+","+file+","+(is_enabled ? "active" : "inactive");
	}
}
